/**
 * jims
 */
package com.jims.master.modules.blood.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jims.master.common.utils.NumberUtils;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.blood.dao.BloodCapacityDao;
import com.jims.master.modules.blood.dao.BloodComponentDao;
import com.jims.master.modules.blood.entity.BloodComponent;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.OrgDept;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.modules.blood.entity.BloodApply;
import com.jims.master.modules.blood.dao.BloodApplyDao;

/**
 * 用血申请Service
 * @author CTQ
 * @version 2017-04-10
 */
@Service
@Transactional(readOnly = true)
public class BloodApplyService extends CrudService<BloodApplyDao, BloodApply> {
	@Autowired
	private BloodCapacityDao bloodCapacityDao;
    @Autowired
    private OrgDeptDao orgDeptDao;
    @Autowired
    private BloodComponentDao bloodComponentDao;

	public BloodApply get(String id) {
		return super.get(id);
	}

	public List<BloodApply> findList(BloodApply bloodApply) {
		return super.findList(bloodApply);
	}

	public Page<BloodApply> findPage(Page<BloodApply> page, BloodApply bloodApply) {
		return super.findPage(page, bloodApply);
	}

	@Transactional(readOnly = false)
	public int saveApply(BloodApply bloodApply) {
		int num = 0;
		User user = UserUtils.getUser();
		bloodApply.setApplyNum(NumberUtils.getHospUseBlood(bloodApply.getVisitId()));
		bloodApply.setAppDept(user.getOrgDept().getId());
		bloodApply.setAppDoctor(user.getId());
		num = super.save(bloodApply);
		return num;
	}

	@Transactional(readOnly = false)
	public int deleteApply(BloodApply bloodApply) {
		bloodCapacityDao.deleteByApplyId(bloodApply.getId());
		return super.delete(bloodApply);
	}

    /**
     * 打印用血申请
     * @param bloodApply
     * @return
     * @author fengyg
     * @date 2017-07-26
     */
    public String printBloodApply(BloodApply bloodApply){
        User user = UserUtils.getUser();
        List lists = new ArrayList();
        bloodApply.updateOrgId();
        BaseDto baseDto = dao.getPatientBloodInfoByApplyNum(bloodApply);  //患者用血申请信息
        List<BaseDto> list = dao.getDiagnosisTypeByVisitId(baseDto.getAsString("visitId"), user.getOrgId());   //患者诊断list集合
        Map map = new HashMap();
        if(null != baseDto){
            map.put("chargeType",DictUtils.getDictLabel(baseDto.getAsString("chargeType"),"CHARGE_TYPE_DICT",""));  //费别
            map.put("bloodInuse",DictUtils.getDictLabel(baseDto.getAsString("bloodInuse"), "BLOOD_FROM_TYPE", ""));  //血源
            map.put("applyNum",baseDto.getAsString("applyNum"));
            map.put("patientName",baseDto.getAsString("patientName"));
            map.put("age",baseDto.getAsString("age"));
            map.put("birthdayDate",baseDto.getAsDate("birthdayDate", "yyyy-MM-dd"));
            map.put("sex",DictUtils.getDictLabel(baseDto.getAsString("sex"),"SEX_DICT",""));
            map.put("visitNo",baseDto.getAsString("visitNo"));
            OrgDept deptAdmissionTo = orgDeptDao.get(baseDto.getAsString("deptAdmissionTo"));  //入院科室
            if(null != deptAdmissionTo){
                map.put("deptAdmissionTo",deptAdmissionTo.getDeptName());
            }
            OrgDept wardAdmissionTo = orgDeptDao.get(baseDto.getAsString("wardAdmissionTo"));
            if(null != wardAdmissionTo){
                map.put("wardAdmissionTo",wardAdmissionTo.getDeptName());  //入院病区
            }
            map.put("bedNo",baseDto.getAsString("bedNo"));  //床号
            if(null != list && list.size() > 0){
                map.put("description",list.get(0).getAsString("description"));  //临床诊断
            }
            map.put("useBloodAim",DictUtils.getDictLabel(baseDto.getAsString("useBloodAim"),"USE_BLOOD_AIM",""));  //输血目的
            map.put("useBloodHistory",DictUtils.getDictLabel(baseDto.getAsString("useBloodHistory"), "USE_BLOOD_HISTORY", ""));  //继往输血史
            map.put("patientColony",baseDto.getAsString("patientColony"));  //受血者属地
            map.put("transDate",baseDto.getAsDate("transDate", "yyyy-MM-dd"));  //预输血时间
            map.put("fastSlow",DictUtils.getDictLabel(baseDto.getAsString("fastSlow"),"FAST_SLOW",""));   //用血安排
            BloodComponent bloodComponent = bloodComponentDao.get(baseDto.getAsString("componentId"));
            if(null != bloodComponent){
                map.put("componentId",bloodComponent.getBloodName());   //用血安排
            }
            map.put("patBloodGroup",DictUtils.getDictLabel(baseDto.getAsString("patBloodGroup"),"BLOOD_TYPE_DICT",""));  //血型
            map.put("rh",baseDto.getAsString("rh"));  //rh
            map.put("hematin",baseDto.getAsString("hematin"));  //血红蛋白
            map.put("leucocyte",baseDto.getAsString("leucocyte"));  //白血球
            map.put("platelet",baseDto.getAsString("platelet"));  //血小板
            map.put("alanineAminotransferase",baseDto.getAsString("alanineAminotransferase"));  //谷丙转氨酶
            map.put("hematocrit",baseDto.getAsString("hematocrit"));  //红细胞压积
            map.put("antibodyToHepatitisC",baseDto.getAsString("antibodyToHepatitisC"));  //丙型肝炎抗体
            map.put("hbsAntigen",baseDto.getAsString("hbsAntigen"));  //乙型肝炎表面抗原
            map.put("hiv",baseDto.getAsString("hiv"));  //人类免疫缺陷病毒
            map.put("syphilis",baseDto.getAsString("syphilis"));  //梅毒
            map.put("applyDate",baseDto.getAsString("applyDate"));   //申请时间
            String hospitalName =  UserUtils.getUser().getCompany().getOrgName();
            map.put("hospitalName", hospitalName);
        }
        lists.add("1");
        String printPath = PrintPdfUtils.previewPdf(user.getId(), "his/blood/printBloodApply.jasper", map, lists, "printBloodApply");
        return  printPath;
    }

}